package main

import (
	"fmt"
	"golang.org/x/net/context"
	"google.golang.org/grpc"
	"log"
	pb "protobuf/trade"
)

const (
	address = "localhost:50051"
)

func main() {
	for i := 0; i < 1; i++ {
		trade()
	}
	//LogList()
}

func tradeList() {
	conn, err := grpc.Dial(address, grpc.WithInsecure())
	if err != nil {
		log.Fatal(err)
	}
	defer conn.Close()
	c := pb.NewDoTradeClient(conn)
	in := &pb.TradeListRequest{
		0,
		0,
		10,
		1,
		3,
	}
	r, err := c.GetUserTradeList(context.Background(), in)
	fmt.Print(len(r.Request))
}

func LogList() {
	conn, err := grpc.Dial(address, grpc.WithInsecure())
	if err != nil {
		log.Fatal(err)
	}
	defer conn.Close()
	c := pb.NewDoTradeClient(conn)
	in := &pb.SystemTradeLogRequest{
		0,
		10,
		1,
	}
	r, err := c.GetSystemTradeLog(context.Background(), in)
	fmt.Print(r)
}
func trade() {
	conn, err := grpc.Dial(address, grpc.WithInsecure())
	if err != nil {
		log.Fatal(err)
	}
	defer conn.Close()

	var sellTrade = map[int]map[string]float64{
		0: map[string]float64{
			"price": 77.76,
			"num":   21,
		},
		1: map[string]float64{
			"price": 77.78,
			"num":   17,
		},
		2: map[string]float64{
			"price": 77.81,
			"num":   23,
		},
		3: map[string]float64{
			"price": 77.85,
			"num":   16,
		},
		4: map[string]float64{
			"price": 77.86,
			"num":   19,
		},
	}

	var buyTrade = map[int]map[string]float64{
		0: map[string]float64{
			"price": 77.70,
			"num":   14,
		},
		1: map[string]float64{
			"price": 77.68,
			"num":   28,
		},
		2: map[string]float64{
			"price": 77.65,
			"num":   30,
		},
		3: map[string]float64{
			"price": 77.34,
			"num":   63,
		},
		4: map[string]float64{
			"price": 77.10,
			"num":   29,
		},
	}
	//for _, value := range sellTrade {
	//	Sell(conn, value)
	//	time.Sleep(1 * time.Second)
	//}
	//for _, value := range buyTrade {
	//	Buy(conn, value)
	//	time.Sleep(1 * time.Second)
	//}
	var _ = sellTrade
	var _ = buyTrade

	//n := map[string]float64{
	//	"num":   14,
	//	"price": 77.76,
	//}
	//Buy(conn, n)
	//var n = map[string]float64{
	//	"num":   2,
	//	"price": 77.78,
	//}
	//Buy(conn, n)

	var n = map[string]float64{
		"num":   14,
		"price": 77.70,
	}
	Sell(conn, n)

	//var n = map[string]float64{
	//	"num":   21,
	//	"price": 77.68,
	//}
	//Sell(conn, n)

}

func Buy(conn *grpc.ClientConn, value map[string]float64) {
	c := pb.NewDoTradeClient(conn)
	var t *pb.TradeRequest = &pb.TradeRequest{}
	t.Uid = 1
	t.TotalNum = value["num"]
	t.DealNum = 0
	t.Status = 0
	t.Price = value["price"]
	t.Type = pb.TradeRequest_BUY
	r, err := c.SetTrade(context.Background(), t)
	if err != nil {
		log.Fatal(err, r)
	}
}
func Sell(conn *grpc.ClientConn, value map[string]float64) {
	c := pb.NewDoTradeClient(conn)
	var t *pb.TradeRequest = &pb.TradeRequest{}
	t.Uid = 1
	t.TotalNum = value["num"]
	t.DealNum = 0
	t.Status = 0
	t.Price = value["price"]
	t.Type = pb.TradeRequest_SELL
	r, err := c.SetTrade(context.Background(), t)
	if err != nil {
		log.Fatal(err, r)
	}
}

/*
===========================================
买 |  价格     | 数量
------------------------
1 | 50    |    20
2 | 60   | 30
3 | 49  | 20
===========================================
卖 | 价格  | 数量
1 | 50  | 20
2 |  60 | 30
3 | 60  | 20

===========================================












*/
